var $ = require('../../../lib/jquery');
var util = require('./util');
var template = require('../../tpl/template');
var _header = function () {
    this.init();
}
_header.prototype.init = function () {
var tpl = template('header', {
  data: this.login()
})
$('header').html(tpl);
this.bindevent();
}
_header.prototype.login = function () {
  return !util.getCookie('_login');
}
_header.prototype.bindevent = function () {
  var _this = this;
  $('header').on('click', '.js_user',function(){
    location.href = './user.html'
  })
   $('body').on('click', '.js_logout',function(){
    _this.showTips()
    
  })
  $('body').on('click','.js_search_button',function(){
    let search=$('.js_search_content').val();
    if(search==''){
      alert('搜索不能为空')
    }else{
      util.fetch('api/service/search?search='+search).then(function (res){
       
        if(res.data.length==0){
          alert('暂无数据')
          $('.js_search_content').val('');
          // $('.js_box').html('<p></p>');
        }else{
          $('.js_box').removeClass('hide');
          var tpl = template('searchList', {
            data:res.data
          })
          $('.js_box').html(tpl)
        }
      })
    }
   
  })
  $('body').on('click','.js_to_click',function(){
    var id= $(this).data().id,
    order = $(this).data().order,
    parentid = $(this).data().parentid,
    siteurl = $(this).data().siteurl;
    $('.js_box').addClass('hide');
    if (!!siteurl){
     location.href = siteurl;
    }else if (id == 14 && order == 1 && parentid == 2) {
      location.href = './jyqy.html?id='+id+'&order='+order+'&parentid='+parentid
    } else if (id == 32 && order == 5 && parentid == 5) { // 常用电话
       location.href= './phone.html';
    } else if (id == 10 && order == 5 && parentid == 1){ //健康体检
     location.href='./jktj.html'
    } else if (id == 13 && order == 8 && parentid == 1) { // 地图
     location.href = './map.html';
    } else if (id == 20 && order == 3 && parentid == 3) { // 预防接种
     location.href='./yfjz.html'
    } else if (id == 6 && order == 1 && parentid == 1){ // 只能导诊
     location.href = './zndz.html';
    } else if (id == 6 && order == 1 && parentid == 1){ // 
     location.href= './zytz.html';
    } else if (id == 16 && order == 3 && parentid == 2) {
     location.href = './zwjc.html'
    } else if (id == 15 && order == 2 && parentid == 2) { // 中医提示辨识
     location.href = './zytz.html';
    } else if (id == 7 && order == 2 && parentid == 1) {
      location.href = './yygh.html'
    } else if (id == 12 && order == 7 && parentid == 1) { // 急诊指南
     location.href='./jzzn.html'
    } else if (id == 26 && order == 4 && parentid == 4) {
     location.href = './szjg.html' // 辅助生殖机构查询
    } else if (id == 17 && order == 4 && parentid == 2) {
     location.href ='./mbgl.html'
    } else if (id == 25 && order == 3 && parentid == 4) {
      location.href = './jhsy.html'
    } else if (id == 11 && order == 6 && parentid == 1) {
     location.href = './health.html'
   } else if (id == 9 && order == 4 && parentid == 1) {
     location.href = './bgxq.html'
   } else {
      alert('ui稿尚未提供', JSON.stringify($(this).data()))
    }
  })
  $('body').on('click','.js_logout_config .js_ok_1', function () {
    _this.tipshide();
    sessionStorage.clear();
    location.href = './login.html'
  })
  $('body').on('click','.js_logout_config .js_tips_cancel', function () {
    _this.tipshide();
  })
}
_header.prototype.tpl = '<div class="shadow hide js_logout_config js_confirm"> <div class="confirm_tips pf"> <div class="_title">温馨提示</div> <div class="tip_contemnt"> 您确定要退出吗 </div> <div class="tips_btn"> <span class="cancel js_tips_cancel">取消</span> <span class="js_ok_1">确定</span> </div> </div> </div> '
_header.prototype.showTips = function () {
  var l = $('.js_logout_config').length
  if(l == 0){
    $(this.tpl.replace('hide','')).appendTo($('body'));
  }else{
    $('body').find('.js_logout_config').removeClass('hide');
  }
}
_header.prototype.tipshide =  function () {
  $('body').find('.js_logout_config').addClass('hide');
}

module.exports = _header;

